package cn.ztuo.bitrade.controller.answerTopic;

import cn.ztuo.bitrade.annotation.AccessLog;
import cn.ztuo.bitrade.constant.AdminModule;
import cn.ztuo.bitrade.constant.SysConstant;
import cn.ztuo.bitrade.controller.BaseController;
import cn.ztuo.bitrade.entity.Admin;
import cn.ztuo.bitrade.entity.AnswerTopic;
import cn.ztuo.bitrade.service.AnswerTopicService;
import cn.ztuo.bitrade.util.MessageResult;
import cn.ztuo.bitrade.vo.AnswerTopicVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.time.LocalDateTime;

/**
 * @author: Xiazt
 * @date: 2020-04-22 13:45
 */
@RestController
@RequestMapping("answerTopic")
@Slf4j
public class AnswerTopicController extends BaseController {

    @Autowired
    private AnswerTopicService answerTopicService;

    @RequiresPermissions("answerTopic:page-query")
    @RequestMapping(value = "page-query")
    @AccessLog(module = AdminModule.ANSWERTOPIC, operation = "分页获取题目")
    public MessageResult getByPage(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin,AnswerTopicVO answerTopicVO) {
        log.info("分页获取题目");
        return success(answerTopicService.getByPage(answerTopicVO));
    }

    @RequiresPermissions("answerTopic:save")
    @RequestMapping(value = "saveOrUpdateAnswerTopice")
    @AccessLog(module = AdminModule.ANSWERTOPIC, operation = "添加或修改题目")
    public MessageResult saveAnswerTopic(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, AnswerTopic answerTopic) {
        answerTopic.setCreateTime(LocalDateTime.now());
        AnswerTopic insert = answerTopicService.insert(answerTopic);
        if (insert != null) {
            return success("操作成功");
        } else {
            return error("系统异常");
        }
    }

    @RequiresPermissions("answerTopic:delete")
    @RequestMapping(value = "deleteAnswerTopic")
    @AccessLog(module = AdminModule.ANSWERTOPIC, operation = "删除题目")
    public MessageResult deleteAnswerTopic(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, Long id) {
        log.info("删除题目");
        AnswerTopic answerTopic = answerTopicService.findById(id);
        if (answerTopic == null) {
            return error("id错误");
        } else {
            answerTopicService.delete(id);
            return success("删除成功");
        }
    }

    @RequiresPermissions("answerTopic:getById")
    @RequestMapping(value = "getById")
    @AccessLog(module = AdminModule.ANSWERTOPIC, operation = "根据id查询")
    public MessageResult getById(@SessionAttribute(SysConstant.SESSION_ADMIN) Admin admin, Long id) {
        return success(answerTopicService.findById(id));
    }

}


